﻿using System;
using System.Text;

using GameServer.Managers;
using GameServer.Networking.Packets;

namespace GameServer.Networking.Handlers
{
    class HANDLE_ITEMSHOP : PacketHandler
    {
        public override void Handle(Virtual_Objects.User.virtualUser User)
        {
            try
            {
                if (Program.ESL)
                {
                    User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.CannotBeBougth, "NULL"));
                    return;
                }
                string unknownObj = getNextBlock();
                string WeaponID = getNextBlock();
                int Period = int.Parse(getBlock(4));

                int[] convertDays = new int[4] { 3, 7, 15, 30 };

                virtualItem Item = ItemManager.getItem(WeaponID);
                if (Item != null)
                {
                    if (Item.isValidShopItem && Item.getPrice(Period) >= 0)
                    {
                        if (Item.isPremium && User.Premium <= 0)
                        {
                            User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.PremiumOnly, unknownObj));
                        }
                        else if (LevelCalculator.getLevelforExp(User.Exp) < Item.Level)
                        {
                            User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.LevelLow, unknownObj));
                        }
                        else
                        {
                            int DinarResult = User.Dinar - Item.getPrice(Period);
                            if (DinarResult >= 0)
                            {

                                int InventorySlot = -1;
                                for (int I = 0; I < 30; I++)
                                {
                                    if (User.Inventory[I] == null) { InventorySlot = I; break; }
                                }
                                if (InventorySlot >= 0)
                                {
                                    switch (WeaponID)
                                    {
                                        case "CB03":
                                            {
                                                DB.runQuery("UPDATE users SET kills='0', deaths='0' WHERE id='" + User.UserID + "'");
                                                User.Kills = 0;
                                                User.Deaths = 0;
                                                User.send(new PACKET_CHARACTER_INFO(User));
                                                break;
                                            }
                                        case "CR30":
                                            {
                                                if (InventorySlot < 5)
                                                {
                                                    User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                    return;
                                                }
                                                string _Item1 = "DF95";
                                                string _Item2 = "DE07";
                                                string _Item3 = "DG63";
                                                string _Item4 = "DB63";
                                                string _Item5 = "DJ13";
                                                User.AddItem(_Item1, convertDays[Period]);
                                                User.AddItem(_Item2, convertDays[Period]);
                                                User.AddItem(_Item3, convertDays[Period]);
                                                User.AddItem(_Item4, convertDays[Period]);
                                                User.AddItem(_Item5, convertDays[Period]);
                                                break;
                                            }
                                        default:
                                            {
                                                User.AddItem(WeaponID, convertDays[Period]);
                                                break;
                                            }
                                    }

                                    User.Dinar = DinarResult;
                                    DB.runQuery("UPDATE users SET dinar='" + DinarResult + "' WHERE id=" + User.UserID);

                                    User.reloadInventory();
                                    StringBuilder WeaponList = User.rebuildWeaponList();

                                    User.LoadItems();
                                    User.send(new PACKET_ITEMSHOP(User, WeaponList.ToString().Remove(WeaponList.ToString().Length - 1)));
                                    Log.WriteLine(User.Nickname + " has bought [" + Item.WeaponCode.ToUpper() + "-" + Item.Name + "] for " + convertDays[Period] + "days.");
                                }
                                else
                                {
                                    User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, unknownObj));
                                }
                            }
                            else
                            {
                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.NotEnoughDinar, unknownObj));
                            }
                        }
                    }
                    else
                    {
                        User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.CannotBeBougth, unknownObj));
                    }
                }
                else
                {
                    User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.CannotBeBougth, unknownObj));
                }
            }
            catch { }
        }
    }
}
